package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.product.converter.dto.SaleAttrInfoConverter;
import com.cskaoyan.mall.product.converter.dto.SpuInfoConverter;
import com.cskaoyan.mall.product.converter.dto.SpuInfoPageConverter;
import com.cskaoyan.mall.product.converter.param.SpuInfoParamConverter;
import com.cskaoyan.mall.product.dto.SaleAttrInfoDTO;
import com.cskaoyan.mall.product.dto.SpuInfoPageDTO;
import com.cskaoyan.mall.product.dto.SpuPosterDTO;
import com.cskaoyan.mall.product.mapper.*;
import com.cskaoyan.mall.product.model.*;
import com.cskaoyan.mall.product.query.SpuInfoParam;
import com.cskaoyan.mall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Macro_Ray
 * @since 2024/6/10 下午4:41
 */
@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    SpuInfoConverter spuInfoConverter;
    @Autowired
    SpuInfoParamConverter spuInfoParamConverter;
    @Autowired
    SaleAttrInfoConverter saleAttrInfoConverter;
    @Autowired
    SpuInfoPageConverter spuInfoPageConverter;
    @Autowired
    SaleAttrInfoMapper saleAttrInfoMapper;
    @Autowired
    SkuInfoMapper skuInfoMapper;
    @Autowired
    SpuInfoMapper spuInfoMapper;
    @Autowired
    SpuImageMapper spuImageMapper;
    @Autowired
    SpuPosterMapper spuPosterMapper;
    @Autowired
    SpuSaleAttrInfoMapper spuSaleAttrInfoMapper;
    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Override
    public SpuInfoPageDTO getSpuInfoListByCate3Id(Page<SpuInfo> pageParam, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> spuInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuInfoLambdaQueryWrapper.eq(SpuInfo::getThirdLevelCategoryId, category3Id);
        Page<SpuInfo> spuInfoPage = spuInfoMapper.selectPage(pageParam, spuInfoLambdaQueryWrapper);
        return spuInfoPageConverter.spuInfoPage2PageDTO(spuInfoPage);
    }

    @Override
    public List<SaleAttrInfoDTO> getbaseSaleAttrList() {
        List<SaleAttrInfo> saleAttrInfoList = saleAttrInfoMapper.selectList(null);
        return saleAttrInfoConverter.saleAttributeInfoPOs2DTOs(saleAttrInfoList);
    }

    /**保存商品信息，包括spu_info、spu_image、spu_poster、spu_sale_attr_info、spu_sale_attr_value五张表*/
    @Override
    public void saveSpuInfo(SpuInfoParam spuInfoParam) {
        // spu_info 表
        SpuInfo spuInfo = spuInfoParamConverter.spuInfoParam2Info(spuInfoParam);
        spuInfoMapper.insert(spuInfo);
        // spu_image 表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            for (SpuImage spuImage : spuImageList) {
                spuImage.setSpuId(spuInfo.getId());
                spuImageMapper.insert(spuImage);
            }
        }
        // spu_poster 表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            for (SpuPoster spuPoster : spuPosterList) {
                spuPoster.setSpuId(spuInfo.getId());
                spuPosterMapper.insert(spuPoster);
            }
        }
        // spu_sale_attr_info 表和 spu_sale_attr_value 表
        List<SpuSaleAttrInfo> spuSaleAttrInfoList = spuInfo.getSpuSaleAttrInfoList();
        if (!CollectionUtils.isEmpty(spuSaleAttrInfoList)) {
            for (SpuSaleAttrInfo spuSaleAttrInfo : spuSaleAttrInfoList) {
                spuSaleAttrInfo.setSpuId(spuInfo.getId());
                spuSaleAttrInfoMapper.insert(spuSaleAttrInfo);
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttrInfo.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        spuSaleAttrValue.setSpuSaleAttrId(spuSaleAttrInfo.getId());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    }
                }
            }
        }
    }

    /**获取商品详情页的海报信息*/
    @Override
    public List<SpuPosterDTO> getSpuPosterBySpu(Long spuId) {
        LambdaQueryWrapper<SpuPoster> spuPosterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuPosterLambdaQueryWrapper.eq(SpuPoster::getSpuId, spuId);
        List<SpuPoster> spuPosters = spuPosterMapper.selectList(spuPosterLambdaQueryWrapper);
        return spuInfoConverter.spuPosterPOs2DTOs(spuPosters);
    }
}
